/**
 *
 */
package com.wedroid.r2d2.message.handler;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wedroid.entity.contact.Contacter;
import com.wedroid.entity.contact.Droid;
import com.wedroid.entity.contact.GroupMember;
import com.wedroid.r2d2.cache.PersistentAccessor;
import com.wedroid.r2d2.contact.Contact;
import com.wedroid.r2d2.contact.ContactManager;
import com.wedroid.r2d2.message.RipeMessage;
import com.wedroid.r2d2.resource.ResourceManager;
import com.wedroid.r2d2.service.ContactService;

/**
 * @author lute
 */
@Component("groupMemberIconMessageHandler")
public class GroupMemberIconMessageHandler extends AsyncMessageHandler {

    @Autowired
    private ResourceManager resourceManager;

    @Autowired
    private ContactService contactService;

    @Autowired
    private PersistentAccessor accessor;

    @Autowired
    private ContactManager contactManager;

    /* (non-Javadoc)
     * @see com.wedroid.r2d2.message.handler.AsyncMessageHandler#handleMessageInternal(com.wedroid.r2d2.message.RipeMessage)
     */
    @Override
    protected void handleMessageInternal(RipeMessage ripeMessage) {
        Contact sender = ripeMessage.getSender();
        Contact receiver = ripeMessage.getReceiver();

        // Determine member who sending message and group contact the message sending to
        Contact fromUser = sender.isGroup() ? ripeMessage.getFromUser() : (receiver.isGroup() ? sender : null);
        Contact group = sender.isGroup() ? sender : (receiver.isGroup() ? receiver : null);

        // If member or group is null, do not download group member icon
        if (fromUser == null || group == null) {
            logger.trace("#handleMessageInternal - Group member or group is null, no need to download icon...");
            if (sender.isGroup() || receiver.isGroup()) {
                logger.error("#handleMessageInternal - Sender[{}] or receiver[{}] is group contact.", sender.getNickName(), receiver.getNickName());
            }
            return;
        }

        String fromUserName = fromUser.getUserName();
        String chatRoomId = group.getEncryChatRoomId();
        if (StringUtils.isBlank(fromUserName) || StringUtils.isBlank(chatRoomId)) {
            logger.error("#handleMessageInternal - Member's user name [{}] or chat room id [{}] is blank.", fromUserName, chatRoomId);
            return;
        }

        String iconPath = resourceManager.requestAvatar(fromUser, chatRoomId);
        if (StringUtils.isNotBlank(iconPath)) {
            logger.trace("#handleMessageInternal - Downloaded group member icon in [{}].", iconPath);

            Droid droid = accessor.getDroid(contactManager.getDroid().getNickName());
            Contacter groupContacter = accessor.getContacter(droid, group.getNickName());
            if (groupContacter != null) {
                GroupMember groupMember = contactService.getGroupMember(groupContacter, fromUser.getNickName());
                contactService.updateGroupMember(groupMember.headImgUrl(iconPath));
            } else {
                logger.error("#handleMessageInternal - Group contacter [{}] of droid [{}] not found in database...", droid.getProfile().getNickName(), group.getNickName());
            }
        } else {
            logger.error("#handleMessageInternal - Downloading icon fails...");
        }

    }

}
